<?php
	class ControllerMemoryMessage extends Controller {
		private $error = array();
		public function index() {
			$this->document->setTitle('message');
			$this->document->addStyle('admin/view/stylesheet/docs.min.css');
			$data['maincontent'] = "意见和建议";
			$data['user_id'] = $this->session->data['user_id'];
			$this->load->model('message/message');
			isset($this->request->get['page'])?$page = $this->request->get['page']:$page =1;
			$args = array(
				'mrid'=>0,
				'start'     => ((int)$page -1)*5,
				'limit'     => 5
				);
			$messages = $this->model_message_message->getMessages($args);
			$args = array(
				'mrid'=>1
				);
			$replay   = $this->model_message_message->getMessages($args);
			foreach ($messages as $key => $message) {
				if($messages[$key]['nickname'] ===""){
					$messages[$key]['nickname'] = "无昵称用户";
				}
				if($messages[$key]['rnickname'] ===null){
					$messages[$key]['rnickname'] = "无昵称用户";
				}
				foreach ($replay as $k => $mes) {
					if($mes['nickname'] ===""){
						$mes['nickname'] = "无昵称用户";
					}
					if($mes['rnickname'] ===null||$mes['rnickname'] ===""){
						$mes['rnickname'] = "无昵称用户";
					}
					if($mes['mrid']===$message['id']){
						$messages[$key]['replay'][] = $mes;
					}
				}
			}
			$data['messages'] = $messages;
			$url = "";
			$total = $this->model_message_message->totalMessages();
			//分页开始
			$pagination = new Pagination();
			$pagination->total = (int)$total['total'];
			$pagination->page  = $page;
			$pagination->limit = 5;
			$pagination->url = $this->url->link('memory/message', 'token=' . $this->session->data['token'] . $url.'&page={page}', true);
			$pagination = $pagination->render();
			//分页结束
			$data['pagination']=$pagination; 
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
			}
			$data['test'] = $this->url->link('memory/test', '', true)."&token=".$token;
			$data['addmessageurl'] = $this->url->link('memory/message/addmessage','',true)."&token=".$this->session->data['token'];
			$data['header'] = $this->load->controller('common/headerin');
	 		$data['footerin'] = $this->load->controller('common/footerin');
	 		$data['footer'] = $this->load->controller('common/footer');
			$this->response->setOutput($this->load->view('memory/message', $data));
		}
		public function addmessage() {
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$content = $this->request->post['content'];
				$type = $this->request->post['type'];
				$this->load->model('message/message');
				$data = array(
						"user_id" => $this->session->data['user_id'],
						"content"   => $content,
						"type"	   => $type
						
					);
				if(isset($this->request->post['mrid'])){
					$data['mrid'] = $this->request->post['mrid'];
				}
				if(isset($this->request->post['ruser_id'])){
					$data['ruser_id'] = $this->request->post['ruser_id'];
				}
				$results = $this->model_message_message->addMessage($data);
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($results,0));
			}
		}
		public function getmessages() {
			if($this->request->server['REQUEST_METHOD'] == 'POST') {
				$this->load->model('message/message');
				$results = $this->model_message_message->getMessages();
				header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($results,0));
			}
		}
	}